package cn.yhbk.web.modules.manager.Service.Impl;

import cn.yhbk.web.modules.manager.Mapper.TbStudentMapper;
import cn.yhbk.web.modules.manager.Pojo.DTO.Class.TbClassDto;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbClass;
import cn.yhbk.web.modules.manager.Mapper.TbClassMapper;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbStudent;
import cn.yhbk.web.modules.manager.Pojo.VO.Class.ClassPag;
import cn.yhbk.web.modules.manager.Service.ITbClassService;
import cn.yhbk.web.modules.manager.Service.ITbStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 班级表 服务实现类
 * </p>
 *
 * @author 有何不可
 * @since 2023-08-05
 */
@Service
public class TbClassServiceImpl extends ServiceImpl<TbClassMapper, TbClass> implements ITbClassService {

    @Autowired
    private TbClassMapper tbClassMapper;

    @Autowired
    private TbStudentMapper tbStudentMapper;

    @Lazy
    @Autowired
    private ITbStudentService studentService;




    @Override
    public TbClassDto queryClassInfoById(Long id) {
        return tbClassMapper.queryClassById(id);
    }

    @Override
    public Boolean insertClass(TbClass tbClass) {
        return tbClassMapper.insertClass(tbClass);
    }

    @Override
    public Boolean updateClassById(TbClass tbClass) {
        return tbClassMapper.updateClassById(tbClass);
    }

    @Override
    public Boolean deleteClassById(Long id) {
        return tbClassMapper.deleteClassById(id);
    }

    @Override
    public ClassPag getClassInfoPag(Integer currentPage, Integer pageSize, Long collegeId) {
        ClassPag classPag = new ClassPag();
        classPag.setPageSize(pageSize);
        classPag.setCurrentPage(currentPage);
        classPag.setTotalPage(tbClassMapper.queryTotalNum(collegeId));
        classPag.setClassList(tbClassMapper.queryClassPage((currentPage - 1) * pageSize, pageSize, collegeId));
        return classPag;
    }

    @Override
    public List<TbStudent> getClassStudent(Long classId) {

        return tbStudentMapper.queryStudentByClassId(classId);
    }

    @Override
    public void operatePopulation(int num, Long classId) {
        tbClassMapper.operatePopulation(num,classId);
    }

    @Override
    public void flushPopulation(Long classId) {
        int size = studentService.populationForClass(classId);

        TbClass tbClass = new TbClass();
        tbClass.setId(classId);
        tbClass.setPopulation(size);
        updateById(tbClass);
    }
}
